﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class 参保_add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DateTime d = DateTime.Now;
            this.djsj.Text = d.ToString("yyyy-MM-dd HH:mm:ss");
            if (Session["userinfo"] != null)
            {
                管理员信息表 ad = (管理员信息表)Session["userinfo"];
                this.blr.Text = ad.username;
            }
            else
            {
                this.blr.Text = "匿名管理员";
            }
            
        }
    }
    protected void zhbh_DataBound(object sender, EventArgs e)
    {
        this.GridView1.DataBind();
    }
    protected void zhbh_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.GridView1.DataBind();
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        DateTime date = Convert.ToDateTime(Calendar1.SelectedDate);
        this.jfsj.Text = date.ToString("yyyy-MM-dd");
        this.Calendar1.Visible = false;
        this.Button3.Enabled = true;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        this.Calendar1.Visible = true;
        this.Button3.Enabled = false;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count > 0)
        {
            double sum = 0.0;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                if ("1".Equals(GridView1.Rows[i].Cells[3].Text)) //如果是按月计算
                {
                    double bili = Convert.ToDouble(GridView1.Rows[i].Cells[4].Text); //险种比例
                    double basic = Convert.ToDouble(this.jfgz.SelectedValue);   //年基础工资，平均工资
                    double m = Convert.ToDouble(this.jfys.SelectedValue);       //缴费月数
                    double jf_bili = Convert.ToDouble(this.jfbl.SelectedValue); //基础工资的系数 0.6~3
                    double r = bili * basic * m * jf_bili;
                    GridView1.Rows[i].Cells[5].Text = r.ToString();
                    sum += r;
                }
                else
                {

                    //大病缴费规则是什么？
                    GridView1.Rows[i].Cells[5].Text ="70";
                    sum += 70;
                }
            }
            GridView1.FooterRow.Cells[4].Text = "总计缴费：";
            GridView1.FooterRow.Cells[5].Text = sum.ToString();
        }
    }
    protected void hkxz_DataBound(object sender, EventArgs e)
    {
        if (this.hkxz.Items.Count > 0)
        {
            this.hkxz.Items[0].Selected = true;
        }
    }
    protected void personId_TextChanged(object sender, EventArgs e)
    {
        if (this.personId.Text.Length == 15)
        {
            string bi = this.personId.Text.Substring(7, 6);
            StringBuilder sb = new StringBuilder();
            sb.Append("19");
            sb.Append(bi.Substring(1, 2)).Append("-").Append(bi.Substring(3, 2)).Append("-").Append(bi.Substring(4, 2));
            this.csrq.Text = sb.ToString();
        }
        else if (this.personId.Text.Length == 18)
        {
            string bir = this.personId.Text.Substring(7, 8);
            StringBuilder sbu = new StringBuilder();
            sbu.Append(bir.Substring(1, 4)).Append("-").Append(bir.Substring(3, 2)).Append("-").Append(bir.Substring(4, 2));
            this.csrq.Text = sbu.ToString();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        缴费人员表 jfryb = new 缴费人员表();
        jfryb.personId = this.personId.Text;
        jfryb.insuranceId = this.insuranceId.Text;
        jfryb.username = this.username.Text;
        jfryb.sex = this.sex.SelectedValue;
        jfryb.csrq = Convert.ToDateTime( this.csrq.Text);
        jfryb.mz = this.mz.SelectedValue;
        jfryb.whcd = this.whcd.SelectedValue;
        jfryb.ygxs = this.ygxs.SelectedValue;
        jfryb.grsf = this.grsf.SelectedValue;
        jfryb.hkxz = this.hkxz.SelectedValue;
        jfryb.gsdw = "";
        jfryb.yzbm = this.yzbm.Text;
        jfryb.phone = this.lxdh.Text;
        jfryb.hkdz = this.hkdz.Text;
        jfryb.txdz = this.txdz.Text;
        jfryb.cbzt = this.cbzt.SelectedValue;
        jfryb.del = 0;

        缴费人员表dao j_dao = new 缴费人员表dao();
        缴费人员表dao j_dao2 = new 缴费人员表dao();
        缴费人员表 temp = null;
        if (j_dao.Exists(this.personId.Text))
        {
            /*
            temp = j_dao.GetModel(this.personId.Text);
            if (null != temp)
            {
                temp.del = 1;
                j_dao.Update(temp);
            }
             */
            Response.Redirect("error.aspx?info=已经存在该用户信息");
        }
        else
        {

            j_dao2.Add(jfryb);// 添加一个人员信息

        }





        ArrayList  jfs = new ArrayList ();

        if (GridView1.Rows.Count > 0)
        {

            decimal basic = Convert.ToDecimal(this.jfgz.SelectedValue);   //年基础工资，平均工资
            decimal m = Convert.ToDecimal(this.jfys.SelectedValue);       //缴费月数
            decimal jf_bili = Convert.ToDecimal(this.jfbl.SelectedValue); //基础工资的系数 0.6~3
            
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {                              
                if ("1".Equals(GridView1.Rows[i].Cells[3].Text)) //如果是按月计算
                {
                    for (int j = 0; j < Convert.ToInt32(this.jfys.SelectedValue); j++)
                    {
                        缴费表 t = new 缴费表();
                        t.personId = this.personId.Text;
                        t.insuranceId = this.insuranceId.Text;
                        t.code = this.zhbh.SelectedValue;
                        DateTime d = Convert.ToDateTime(this.jfsj.Text);
                        int year = d.Year;
                        int month = d.Month;
                        int day = d.Day;
                        month += j;
                        string dd_str = year + "-" + month + "-" + day;
                        DateTime dd = Convert.ToDateTime(dd_str);
                        t.jfsj = dd;

                        decimal bili = Convert.ToDecimal(GridView1.Rows[i].Cells[4].Text); //险种比例

                        t.xz = GridView1.Rows[i].Cells[2].Text;
                        t.jfgz = basic;
                        t.jfbl = jf_bili;
                        t.jsbl = bili;
                        t.jsfs = 1;
                        t.jfje = t.jfgz * t.jfbl * t.jsbl;
                        t.del = 0;

                        jfs.Add(t);
                    }
                    
                    
                }
                else
                {

                    for (int j = 0; j < Convert.ToInt32(this.jfys.SelectedValue); j++)
                    {
                        缴费表 t = new 缴费表();
                        t.personId = this.personId.Text;
                        t.insuranceId = this.insuranceId.Text;
                        t.code = this.zhbh.SelectedValue;
                        DateTime d = Convert.ToDateTime(this.jfsj.Text);
                        int year = d.Year;
                        int month = d.Month;
                        int day = d.Day;
                        month += j;
                        string dd_str = year + "-" + month + "-" + day;
                        DateTime dd = Convert.ToDateTime(dd_str);
                        t.jfsj = dd;

                        decimal bili = Convert.ToDecimal(GridView1.Rows[i].Cells[4].Text); //险种比例

                        t.xz = GridView1.Rows[i].Cells[2].Text;
                        t.jfgz = basic;
                        t.jfbl = jf_bili;
                        t.jsbl = bili;
                        t.jsfs = 2;
                        t.jfje = 70;//大病缴费规则是什么？
                        t.del = 0;

                        jfs.Add(t);
                    }
                    
                    
                }
            }
            缴费表dao jf_dao = new 缴费表dao();
                       
            缴费表[] jfs_ = (缴费表[])jfs.ToArray(typeof(缴费表));
            int count = jf_dao.bulckExecute(jfs_);

            this.Label1.Text = "count:" + count;
            
        }

    }
}
